package cn.change.user.service.impl;
import cn.change.basic.contants.BaseConstants;
import cn.change.basic.dto.RegisterDto;
import cn.change.basic.exception.BusinssException;
import cn.change.basic.service.impl.BaseServiceImpl;
import cn.change.basic.util.MD5Utils;
import cn.change.basic.util.StrUtils;
import cn.change.user.domain.Logininfo;
import cn.change.user.domain.User;
import cn.change.user.mapper.LogininfoMapper;
import cn.change.user.mapper.UserMapper;
import cn.change.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhl
 * @since 2022-04-01
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void phoneRegister(RegisterDto dto) {
        //1.校验 @TODO 自研
        //2.两次密码是否一致
        if(!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new BusinssException("密码都不一致，没有资格！！！");
        }
        //3.校验短信验证码
        String smsCodeKey = BaseConstants.VerifyCodeConstants.BUSINESS_REGISTER_PREFIX+dto.getPhone();
        Object smsCodeValueTmp = redisTemplate.opsForValue().get(smsCodeKey);//9527:465432132321
        if(smsCodeValueTmp == null){
            throw new BusinssException("验证码已过期，请重新获取！！！");
        }
        String smsCodeValue = smsCodeValueTmp.toString().split(":")[0];//9527
        if(!smsCodeValue.equalsIgnoreCase(dto.getSmsCode())){
            throw new BusinssException("验证码错误！！");
        }
        //初始化user
        User user = dto2User(dto);
        //user拷贝成logininfo
        Logininfo logininfo = user2Logininfo(user);
        //user + logininfo
        logininfoMapper.save(logininfo);//返回自增id
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);//一定要设置
        return logininfo;
    }


    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    /**
     * 我们在CUD user表的时候，需要级联操作logininfo，所以我们需要复写 save update  remove
     */
    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }
}
